package com.itheima.a01mystream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.function.Function;
import java.util.stream.Stream;

public class StreamDemo3 {
    public static void main(String[] args) {
        //stream流的在中间方法

        //需要过滤的数据
        ArrayList<String> list = new ArrayList<String>();
        Collections.addAll(list, "A-1", "AB", "C-2","C","C", "AD","AD-3", "AAE", "F", "G");
        ArrayList<Integer> list2 = new ArrayList<Integer>();
        Collections.addAll(list2,  1,3);


        //filter 过滤
        list.stream().filter(s->s.startsWith("A")).forEach(s->System.out.println(s));

        //limit 获取前几个元素
        System.out.println("====================");
        list.stream().limit(3).forEach(s->System.out.println(s));

        //skip 跳过前几个元素
        System.out.println("===================");
        list.stream().skip(3).forEach(s->System.out.println(s));

        //distinct 元素去重,需要重写HashCode方法和equals方法
        System.out.println("===================");
        list.stream().distinct().forEach(s->System.out.println(s));

        //concat 合并两个流为一个流，这是一个stream的静态方法
        //合并时尽可能保持两个流里面的数据类型一致，否则会自动进行数据类型提升，导致无法使用此类里面的特有功能
        System.out.println("===================");
        Stream.concat(list.stream(),list2.stream()).forEach(s->System.out.println(s));


        ArrayList<String> list3 = new ArrayList<String>();
        Collections.addAll(list3, "A-1", "AB-3", "C-2");
        //map 转换流中的数据类型
        System.out.println("===================");
        list3.stream().map(new Function<String, Object>() {
            @Override
            public Object apply(String s) {
                String s1 = s.split("-")[1];
                int i = Integer.parseInt(s1);

                return i;
            }
        }).forEach(s->System.out.println(s));

        System.out.println("===================");
        list3.stream().map(s->Integer.parseInt(s.split("-")[1])).forEach(s->System.out.println(s));
    }
}
